/*
 * NewMapDialog.java
 *
 * Created on 5 de Abril de 2007, 10:58
 */

package org.gpp.proj1.map.editor;

import javax.swing.JOptionPane;
import javax.swing.ListModel;

/**
 *
 * @author  Gustavo
 */
public class NewMapDialog extends javax.swing.JDialog {
    
    /**
     *
     */
    private static final long serialVersionUID = -750702058934157471L;
    
    private ListModel listModel;
    private MapCreationHandler handler;

    /** Creates new form NewMapDialog */
    public NewMapDialog(java.awt.Frame parent, boolean modal, MapCreationHandler handler, ListModel listModel) {
        super(parent, modal);
        
        this.listModel = listModel;
        initComponents();
        this.setLocationRelativeTo(parent);

        this.handler = handler;
    }
    
    private int getTopTexture() {
        return this.topTextureList.getSelectedIndex();
    }
    
    private int getSideTexture() {
        return this.sideTextureList.getSelectedIndex();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        accessibilityButtonGroup = new javax.swing.ButtonGroup();
        createButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        widthDimensionTextField = new javax.swing.JTextField();
        lengthDimensionTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        topTextureList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        sideTextureList = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        heightTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        accessibleRadioButton = new javax.swing.JRadioButton();
        inaccessibleRadioButton = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New map");
        setModal(true);
        setResizable(false);
        createButton.setText("Create");
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Select main map settings:");

        widthDimensionTextField.setText("15");
        widthDimensionTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                widthDimensionTextFieldFocusGained(evt);
            }
        });

        lengthDimensionTextField.setText("15");
        lengthDimensionTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                lengthDimensionTextFieldFocusGained(evt);
            }
        });

        jLabel2.setText("Dimensions:");

        jLabel3.setText("blocks");

        topTextureList.setModel(this.listModel);
        topTextureList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        topTextureList.setSelectedIndex(0);
        topTextureList.setVisibleRowCount(2);
        topTextureList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                topTextureListValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(topTextureList);

        sideTextureList.setModel(this.listModel);
        sideTextureList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        sideTextureList.setSelectedIndex(0);
        sideTextureList.setVisibleRowCount(2);
        sideTextureList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                sideTextureListValueChanged(evt);
            }
        });

        jScrollPane2.setViewportView(sideTextureList);

        jLabel4.setText("X");

        jLabel5.setText("Initial blocks height:");

        heightTextField.setText("0");
        heightTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                heightTextFieldFocusGained(evt);
            }
        });

        jLabel6.setText("Initial top texture:");

        jLabel7.setText("Initial side texture:");

        jLabel8.setText("Initial blocks accessibility:");

        accessibilityButtonGroup.add(accessibleRadioButton);
        accessibleRadioButton.setSelected(true);
        accessibleRadioButton.setText("Accessible");
        accessibleRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        accessibleRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        accessibilityButtonGroup.add(inaccessibleRadioButton);
        inaccessibleRadioButton.setText("Inaccessible");
        inaccessibleRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        inaccessibleRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(layout.createSequentialGroup()
                                .add(jLabel2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(widthDimensionTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 45, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel4)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lengthDimensionTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 45, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel3))
                            .add(layout.createSequentialGroup()
                                .add(jLabel5)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(heightTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 45, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(166, 166, 166))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(createButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 224, Short.MAX_VALUE)
                                .add(cancelButton))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel6)
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 131, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 90, Short.MAX_VALUE)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel7)
                                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 133, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(jLabel8)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(inaccessibleRadioButton)
                            .add(accessibleRadioButton))
                        .addContainerGap(164, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(27, 27, 27)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(widthDimensionTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4)
                    .add(lengthDimensionTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .add(19, 19, 19)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(heightTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(23, 23, 23)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel8)
                    .add(accessibleRadioButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(inaccessibleRadioButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 21, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(jLabel7))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jScrollPane2)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(21, 21, 21)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(createButton)
                    .add(cancelButton))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void heightTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_heightTextFieldFocusGained
        this.heightTextField.selectAll();
    }//GEN-LAST:event_heightTextFieldFocusGained

    private void lengthDimensionTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_lengthDimensionTextFieldFocusGained
        this.lengthDimensionTextField.selectAll();
    }//GEN-LAST:event_lengthDimensionTextFieldFocusGained

    private void widthDimensionTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_widthDimensionTextFieldFocusGained
        this.widthDimensionTextField.selectAll();
    }//GEN-LAST:event_widthDimensionTextFieldFocusGained

    private void sideTextureListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_sideTextureListValueChanged
        this.configureCreateButton();
    }//GEN-LAST:event_sideTextureListValueChanged

    private void topTextureListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_topTextureListValueChanged
        this.configureCreateButton();
    }//GEN-LAST:event_topTextureListValueChanged

    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        
        try {
            this.handler.createMap(this.getMapWidth(), this.getMapLength(), this.getBlockHeight(), this.getTopTexture(), this.getSideTexture(), this.getModifier());
            this.dispose();
            
        } catch (IllegalArgumentException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_createButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private int getMapLength() throws IllegalArgumentException {
        int value;
        try {
            value = Integer.parseInt(this.lengthDimensionTextField.getText());
        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException("Invalid length.");
        }
        
        if (value < 1 || value > EditorProperties.getInstance().max_map_length) {
            throw new IllegalArgumentException("Invalid length. Try 1-" + EditorProperties.getInstance().max_map_length + ".");
        }
        
        return value;
    }

    private int getMapWidth() throws IllegalArgumentException {
        
        int value;
        try {
            value = Integer.parseInt(this.widthDimensionTextField.getText());
        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException("Invalid width.");
        }
        
        if (value < 1 || value > EditorProperties.getInstance().max_map_width) {
            throw new IllegalArgumentException("Invalid width. Try 1-" + EditorProperties.getInstance().max_map_width + ".");
        }
        
        return value;
    }

    private int getBlockHeight() throws IllegalArgumentException {
        
        int value;
        try {
            value = Integer.parseInt(this.heightTextField.getText());
        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException("Invalid height.");
        }
        
        if (value < 0 || value > EditorProperties.getInstance().max_block_height) {
            throw new IllegalArgumentException("Invalid height. Try 0-" + EditorProperties.getInstance().max_block_height + ".");
        }
        
        return value;
    }

    private void configureCreateButton() {
        if (this.topTextureList.getSelectedIndex() != -1 && this.sideTextureList.getSelectedIndex() != -1) {
            this.createButton.setEnabled(true);
        }
        else {
            this.createButton.setEnabled(false);
        }
    }

    private byte getModifier() {
        return this.accessibleRadioButton.isSelected() ? EditorProperties.getInstance().default_accessibility_index :
               this.inaccessibleRadioButton.isSelected() ? EditorProperties.getInstance().inaccessible_block_index : EditorProperties.getInstance().default_accessibility_index;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup accessibilityButtonGroup;
    private javax.swing.JRadioButton accessibleRadioButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton createButton;
    private javax.swing.JTextField heightTextField;
    private javax.swing.JRadioButton inaccessibleRadioButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField lengthDimensionTextField;
    private javax.swing.JList sideTextureList;
    private javax.swing.JList topTextureList;
    private javax.swing.JTextField widthDimensionTextField;
    // End of variables declaration//GEN-END:variables
    
}
